package com.xmy.study.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class DateUtil {
    public static Integer getNowTime(){
        Integer tt = (int) (System.currentTimeMillis() / 1000);
        return tt;
    }

    public static LocalDate getNowDate(){
        /*Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        //return dateFormat.format(calendar.getTime());
        return calendar.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();*/
        return LocalDate.now();
    }

    public static LocalDateTime getNowDateTime(){
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
        return localDateTime;
    }

    public static LocalDate StrToDate(String pDate) throws ParseException {
        LocalDate date = LocalDate.parse(pDate);
        return date;
    }

    public static LocalDateTime StrToDateTime(String pDateTime) throws ParseException {
        return LocalDateTime.parse(pDateTime);
    }

    /**
     * 通过秒毫秒数判断两个时间的间隔的天数
     * @param date1
     * @param date2
     * @return
     */
    public static Long diffDays(LocalDate date1,LocalDate date2)
    {
        //int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        //return days;

        //return date1.until(date2).getDays();
        return date1.until(date2, ChronoUnit.DAYS);
    }

    public static Integer getWeekIndex(LocalDate date){
        //String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        Calendar cal = Calendar.getInstance();
        Date date1 = Date.from(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        cal.setTime(date1);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        //System.out.print("week_index:"+week_index);
        if(week_index<0){
            week_index = 0;
        }
        //return weeks[week_index];
        return week_index;
    }

    public static String getWeekStr(String weekIndexs){
        String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        String weekStr = "";
        if (StringUtils.isNotBlank(weekIndexs)){
            String[] stringArray = weekIndexs.split(",");
            List<String> weekList = new ArrayList<>();
            for (String str : stringArray) {
                Integer number = Integer.parseInt(str);
                weekList.add(weeks[number]);
            }
            Collections.sort(weekList, new WeekdayComparator());
            weekStr = String.join(",", weekList);
        }
        return weekStr;
    }

    public static void addDates(Date startDate,Integer d) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);//设置起时间
        //calendar.add(Calendar.YEAR, 1);//增加一年
        calendar.add(Calendar.DATE, d);//增加1天
        //calendar.add(Calendar.DATE, -1);//减1天
        //calendar.add(Calendar.MONTH, 1);//增加一个月
        Date dt = calendar.getTime();
        //SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        //String date =  dateFormat.format(dt);
        //System.out.print("date:"+date);
    }

    public static LocalDate getMonDay(){
        // 获取当天日期
        LocalDate now = LocalDate.now();
        // 周一
        LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        return monday;
    }

    public static  LocalDate getSunDay(){
        // 获取当天日期
        LocalDate now = LocalDate.now();
        // 周日
        LocalDate sunday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return sunday;
    }


}
